///引入共享头部
import '../../../common/shard.dart';

import 'package:video_thumbnail/video_thumbnail.dart';
import 'package:path_provider/path_provider.dart';

import '../../../widget/media_preview.dart';

class MediaPreviewPage extends StatelessWidget {
	const MediaPreviewPage({Key? key}) : super(key: key);

	Future loadVideoThumb(String url)async{
        String? fileName = await VideoThumbnail.thumbnailFile(
            video: url,
            thumbnailPath: (await getTemporaryDirectory()).path,
            imageFormat: ImageFormat.WEBP,
            maxWidth:400,
            quality: 75,
        );
        return Future.value(fileName);
    }

	@override
	Widget build(BuildContext context) {
		String image="http://c-ssl.duitang.com/uploads/item/201808/21/20180821223915_yyrpi.thumb.400_0.jpg";
		String video="https://www.runoob.com/try/demo_source/movie.mp4";

		return CustomCupertinoScaffold(
			titleText: "媒体播放器",
			navBackgroundColor: Theme.of(context).primaryColor,
			body: Column(
				children:  [
					const SizedBox(height:100),
					const Text("点击图片"),
					SizedBox(
						child: CacheImage(
							hero: "image",
							url: image,
							onTap: (){
								List<MediaItem>mediaItems=[
									MediaItem(
										id:"image",
										url:image
									)
								];
								openMediaPreview(context: context, mediaItems: mediaItems);
							},
						),
						width: 200,
						height: 200,
					),
					const SizedBox(height:100),
					const Text("点击视频"),
					SizedBox(
						child:FutureBuilder(
							future: loadVideoThumb(video),
							builder: (BuildContext context, AsyncSnapshot snapshot) {
								switch (snapshot.connectionState) {
									case ConnectionState.done:
										return Stack(
											children: [
												Positioned.fill(child: CacheImage(
													fit: BoxFit.cover,
													url: snapshot.data,
													hero:'video',
													onTap: (){
														List<MediaItem>mediaItems=[
															MediaItem(
																id:"video",
																url:video,
																type: 'video',
																cover: snapshot.data
															)
														];
														openMediaPreview(context: context, mediaItems: mediaItems);
													},
												)),
												const Positioned.fill(
													child: Center(
														child: Icon(Icons.play_circle_fill,color: Colors.white),
													),
												)
											],
										);
									default:
										return Container();
								}

							},
						),
						width:200,
						height:200*(9/16),
					)
				],
			),
		);
	}
}
